package com.smart.common.commder;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.alibaba.fastjson2.JSONObject;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.manager.finance.repository.AccountWxpayRepository;
import com.smart.manager.system.entities.models.SystemApi;
import com.smart.manager.system.entities.models.SystemDict;
import com.smart.manager.system.repository.ApiRepository;
import com.smart.manager.system.repository.ConfigRepository;
import com.smart.manager.system.repository.DictRepository;
import com.smart.manager.system.repository.ScheduleAppRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenOAuth2ServiceImpl;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Xiaxuqiu
 * 系统模块启动执行
 * @date 2022-07-02 init
 **/
@Log4j2
@Order(value = -999)
@RequiredArgsConstructor
@Component
public class SmartCmdRunner implements CommandLineRunner {

    private final SmartScanner smartScanner;
    private final DictRepository dictRepository;
    private final ApiRepository apiRepository;
    private final ScheduleAppRepository scheduleAppRepository;
    private final AccountWxpayRepository accountWxpayRepository;
    private final ConfigRepository configRepository;




    private final WxOpenOAuth2ServiceImpl wxOpenOAuth2Service;
    private final WxMaService wxMaService;

    /**
     * 对比更新模块数据
     */
    @Override
    public void run(String... args) {
        log.info("正在扫描系统接口......");
        List<SystemApi> allApi = smartScanner.scanAllApi();
        apiRepository.contrastUpdate(allApi);
        log.info("正在扫描系统枚举......");
        List<SystemDict> currentAllEnumDict = smartScanner.sanAllEnum();
        dictRepository.contrastUpdate(currentAllEnumDict);
        log.info("正在扫描定时任务......");
        scheduleAppRepository.scan();
        log.info("正在更新微信支付配置......");
        accountWxpayRepository.updateWechatPayConfig();

        initWxMaConfig();
        initWxOpenConfig();

    }


    public void initWxOpenConfig() {
        JSONObject jsonObject = configRepository.readCache(SmartConstant.WEIXIN_OPEN_CFG);
        if (jsonObject.isEmpty()) {
            log.error("没有解析到微信开放平台的配置参数");
            return;
        }

        WxOpenInMemoryConfigStorage wxOpenInMemoryConfigStorage = new WxOpenInMemoryConfigStorage();
        String appId = jsonObject.getString("appId");
        String appSecret = jsonObject.getString("appSecret");
        wxOpenInMemoryConfigStorage.setComponentAppId(appId);
        wxOpenInMemoryConfigStorage.setComponentAppSecret(appSecret);

        wxOpenOAuth2Service.setWxOpenConfigStorage(wxOpenInMemoryConfigStorage);
    }



    public void initWxMaConfig() {
        JSONObject jsonObject = configRepository.readCache(SmartConstant.WEIXIN_MA_CFG);
        if (jsonObject.isEmpty()) {
            log.error("没有解析到微信小程序的配置参数");
            return;
        }
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        String appId = jsonObject.getString("appId");
        String appSecret = jsonObject.getString("appSecret");
        log.warn("appid = {} , appSecret = {}", appId, appSecret);
        config.setAppid(appId);
        config.setSecret(appSecret);
        wxMaService.setWxMaConfig(config);
    }
}
